﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;

// 战斗逻辑控制
public class FightSceneLogic
{
    // 场景摄像机
    protected Camera _mainCamera;

    // 场景坐标原点
    protected Vector3 _origin;
    // 场景右上角坐标
    protected Vector3 _topRight;

    // 所有场景内的物体，都应放在该节点下
    protected Transform _sceneRoot;

    // 背景
    protected Transform _bgRoot;

    // 植物放置格子
    protected Transform _gridRoot;

    // 僵尸起点(只用X坐标)
    protected Transform _zombieStart;

    // 当前待种植物
    protected int _selectedPlantID = -1;

    protected bool isGrowing { get { return _selectedPlantID > 0; } }

    // 种植结束
    protected Action OnGrowEnd;
    
    protected GridMgr _gridMgr = new GridMgr();// 负责格子计算
    AttackLineMgr _attackLineMgr; // 进攻线
    ZombieMgr _curZombieMgr; // 僵尸管理
                             // 植物管理

    public void Init()
    {
        MapLoader.instance.LoadMap("FightScene", onLoadEnd);

        // 挂在UI事件
        FightUIMgr.instance.SetPlantList(new List<int>() { 1, 2 });
        FightUIMgr.instance.SetTouchSceneCallback(onClickInScene);
        FightUIMgr.instance.SetSelectPlantToGrowCallback(onSelectPlant);
        FightUIMgr.instance.FillGrowEndCallback(ref OnGrowEnd);
    }

    // 选中了待种植物
    private void onSelectPlant(int plantID)
    {
        _selectedPlantID = plantID;
    }

    // 场景加载结束
    private void onLoadEnd()
    {
        // 顺序要慎重修改
        initScene();
        initAttackLine();
        initZombie(_attackLineMgr.AllLine);
    }

    private void initAttackLine()
    {
        _attackLineMgr = new AttackLineMgr();

        var c = _gridMgr.GetGridCenter(new Vector2(0, 0));
        var list = new List<Vector3>();
        list.Add(new Vector3(_zombieStart.position.x, c.y - 0.666f));
        c = _gridMgr.GetGridCenter(new Vector2(0, 1));
        list.Add(new Vector3(_zombieStart.position.x, c.y - 0.666f));
        c = _gridMgr.GetGridCenter(new Vector2(0, 2));
        list.Add(new Vector3(_zombieStart.position.x, c.y - 0.666f));

        _attackLineMgr.Init(_gridMgr,list);
    }

    // 初始化僵尸
    private void initZombie(List<AttackLine> allLines)
    {
        _curZombieMgr = new ZombieMgr();
        _curZombieMgr.Init(allLines);
    }

    /// <summary>
    /// 初始化场景组织结构
    /// </summary>
    void initScene()
    {
        // 相机
        _mainCamera = UIManager.instance.MainCamera;
        _origin = _mainCamera.ScreenToWorldPoint(Vector3.zero);
        _topRight = _mainCamera.ScreenToWorldPoint(new Vector3(_mainCamera.pixelWidth, _mainCamera.pixelHeight, 0));

        // 逻辑根节点
        var root = ResMgr.instance.InstanceGameObject("FightScene/SceneRoot");
        _sceneRoot = root.transform;
        _sceneRoot.position = _origin + new Vector3(0, 0, 10);

        // 添加背景
        _bgRoot = ResMgr.instance.InstanceGameObject("FightScene/BGRoot").transform;
        _bgRoot.SetParent(_sceneRoot, false);
        // 添加格子信息
        _gridRoot = ResMgr.instance.InstanceGameObject("FightScene/Grid").transform;
        _gridRoot.SetParent(_sceneRoot, false);

        var girdRightTop = _gridRoot.Find("GridRightTop").position;

        _gridMgr.SetGridInfo(_gridRoot.transform.position, girdRightTop, Global.MaxRow, Global.MaxColum, _sceneRoot);

        // 用于编辑格子分布及位置
        //TimerMgr.instance.CreateFrameBaseTimerAndStart(() =>
        //{
        //    _gridMgr.SetGridInfo(_gridRoot.transform.position, _gridRoot.Find("GridRightTop").position, Global.MaxRow, Global.MaxColum, _sceneRoot);
        //});

        // 添加僵尸起点
        _zombieStart = ResMgr.instance.InstanceGameObject("FightScene/ZombieStart").transform;
        _zombieStart.SetParent(_sceneRoot, false);
    }

    private void onClickInScene(Vector3 mousePosition)
    {
        if (!isGrowing) { Debug.Log("请先选择植物"); return; }

        var grid = _gridMgr.GetGrid(_mainCamera.ScreenToWorldPoint(mousePosition));
        if (grid == null) { return; }
        onClickGrid(grid);
    }

    // 种植结束
    private void growEnd()
    {
        _selectedPlantID = -1;
        if (OnGrowEnd != null) { OnGrowEnd(); }
    }

    // 点击了地面格子
    private void onClickGrid(Grid grid)
    {
        if (grid.CanGrow)
        {
            var plantData = PlantTable.instance[_selectedPlantID];
            Type type = Type.GetType(plantData.Script);
            var plant = type.GetConstructor(new Type[] { }).Invoke(null) as PlantInScene;
            plant.Init(plantData);
            plant.GrowInGrid(grid);

            // 通知，种植结束
            growEnd();
        }
        else
        {
            Debug.Log("滴滴，不可种植");
            //grid.RemovePlant();
        }

    }
}
